/**
 * 
 */
package edu.arizona.bio5.onto.decomposition.persistent.file;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import edu.arizona.bio5.onto.decomposition.persistent.ADPersistenceException;
import edu.arizona.bio5.onto.decomposition.persistent.PersistentAtomicDecomposition;

/**
 * Loads each next atomic decomposition
 *
 * (c) Pavel Klinov
 *
 */
public class DiskADIterator implements Iterator<PersistentAtomicDecomposition> {

	private List<File> m_adFolders = null;
	private Iterator<File> m_adIter = null;
	private File m_currFolder = null;
	
	public DiskADIterator(File mainFolder) {
		
		m_adFolders = new ArrayList<File>();
		
		for (File file : mainFolder.listFiles()) {
			//Assume that every directory stores an AD
			//May add some heuristic check to detect other directories
			if (file.isDirectory()) m_adFolders.add(file);
		}
		
		m_adIter = m_adFolders.iterator();
	}
	
	@Override
	public boolean hasNext() {

		return m_adIter.hasNext();
	}

	@Override
	public PersistentAtomicDecomposition next() {

		DiskADLoader loader = new DiskADLoader(m_currFolder = m_adIter.next());
		PersistentAtomicDecomposition ad = null;
		
		try {
			
			ad = loader.load();
			
		} catch (ADPersistenceException e) {
			//AD couldn't be loaded
			e.printStackTrace();
			//TODO Throw a runtime exception or return null?
			return null;
		}
		
		return ad;
	}
	
	public File getCurrentFolder() {
		
		return m_currFolder;
	}

	@Override
	public void remove() {
		throw new UnsupportedOperationException();
	}
}